

set more off

***Set working directory - end users will want to change this
global workdir "E:\SRTR\HCV\replication kit\" 

set scheme s1mono
set graphics on
graph set window fontface "Times New Roman"
graph set eps fontface Times

cd "$workdir\output\"

cap log close
log using "$workdir\logs\regressions - KI DiD.log", text replace


****************************************
****** WL Addition Analyses ************
****************************************

use "$workdir\outputdata\estimation sample.dta", clear


//drop if wl_org=="KI"
drop if !inrange(can_listing_year,2005,2019)
drop if missing(can_listing_opo_id)
drop if wl_org=="KI" & can_dgn_hep==1 // drops any KI candidates that were marked as HCV-related diagnosis in data prep file
replace can_dgn_hep = 1 if wl_org=="KI"
drop if missing(can_dgn_hep) // this drops LI candidates with diagnosis codes that are neither classified as hcv or non-hcv


gen wladd = 1 if !missing(wl_org)

collapse (sum) wladd, by(can_dgn_hep wl_org can_listing_opo_id can_listing_year)

fillin can_dgn_hep wl_org can_listing_opo_id can_listing_year

drop if _fillin==1 & wl_org=="KI" & can_dgn_hep==0 // no need to have HCV/non-HCV for KI 

* drop OPOs that never have LI activity
bysort can_listing_opo_id wl_org: egen avgfillin = mean(_fillin)
drop if avgfillin==1 & wl_org=="LI" // this drops 5 OPOs: 416, 422, 427, 460, 471


foreach x in wladd {
	replace `x' = 0 if missing(`x')
	gen ln`x' = ln(1+`x')
}

encode wl_org, gen(wl_org1)

** making it so that KI is retained as control group when specifying HCV vs. Non-HCV groups in regressions
gen can_dgn_nothep = 0 if wl_org=="LI" & can_dgn_hep==1
replace can_dgn_nothep = 1 if wl_org=="KI"
replace can_dgn_nothep = 1 if can_dgn_hep==0 & wl_org=="LI"

gen year = can_listing_year

gen daa = 0
replace daa = 1 if year>=2014 & wl_org=="LI"

sort can_listing_opo_id can_listing_year wl_org can_dgn_hep

foreach x in 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 {
gen treated`x' = 0
replace treated`x' = 1 if wl_org=="LI" & year==`x'
} 

gen zero = 0

set scheme s1mono
set graphics on // set on to view graphics
graph set window fontface "Times New Roman" // Looks good in Journals
graph set eps fontface Times // Looks Good in Journals


**************************************
*** Figure 5b and 5d, and... *********
*** ... Table 2, column 3 ************
**************************************

*initialize outreg file
global outregfilename wladditions_dsayear_KI_DiD
reg wladd year
outreg2 using $outregfilename.xls, excel stats(coef se) keep(year) dec(4) pdec(4) replace

foreach x in wladd {
 
	reghdfe ln`x' daa if can_dgn_hep==1, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa) dec(4) pdec(4) append
	
	sum `x' if wl_org=="LI" & e(sample)
	sum `x' if wl_org=="LI" & e(sample) & inrange(year,2005,2013)
	
	reghdfe ln`x' treated2005 treated2006 treated2007 treated2008 treated2009 treated2010 treated2011 zero treated2013 ///
	treated2014 treated2015 treated2016 treated2017 treated2018 treated2019 if can_dgn_hep==1, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append
	
	est sto ln`x'_hcv
	coefplot ln`x'_hcv, omitted keep(treated2005 treated2006 treated2007 treated2008 treated2009 treated2010 treated2011 zero treated2013 treated2014 treated2015 treated2016 treated2017 treated2018 treated2019) ///
	coeflabel(treated2005 = "2005" treated2006 = "2006" treated2007 = "2007" treated2008 = "2008" treated2009 = "2009" treated2010 = "2010" treated2011 = "2011" zero = "2012" treated2013 = "2013" ///
	treated2014 = "2014" treated2015 = "2015" treated2016 = "2016" treated2017 = "2017" treated2018 = "2018" treated2019 = "2019") ///
	vertical yline(0) xline(9.5) title("Effect of DAAs on Log HCV WL Additions") xtitle(Year) ytitle("Log Number of WL Additions")
	graph export "ln`x'_HCV_ES.eps", as(eps) replace

	
	reghdfe ln`x' daa if can_dgn_nothep==1, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa) dec(4) pdec(4) append

	sum `x' if wl_org=="LI" & e(sample)
	sum `x' if wl_org=="LI" & e(sample) & inrange(year,2005,2013)

	reghdfe ln`x' treated2005 treated2006 treated2007 treated2008 treated2009 treated2010 treated2011 zero treated2013 ///
	treated2014 treated2015 treated2016 treated2017 treated2018 treated2019 if can_dgn_nothep==1, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append
	
	est sto ln`x'_nonhcv
	coefplot ln`x'_nonhcv, omitted keep(treated2005 treated2006 treated2007 treated2008 treated2009 treated2010 treated2011 zero treated2013 treated2014 treated2015 treated2016 treated2017 treated2018 treated2019) ///
	coeflabel(treated2005 = "2005" treated2006 = "2006" treated2007 = "2007" treated2008 = "2008" treated2009 = "2009" treated2010 = "2010" treated2011 = "2011" zero = "2012" treated2013 = "2013" ///
	treated2014 = "2014" treated2015 = "2015" treated2016 = "2016" treated2017 = "2017" treated2018 = "2018" treated2019 = "2019") ///
	vertical yline(0) xline(9.5) title("Effect of DAAs on Log Non-HCV WL Additions") xtitle(Year) ytitle("Log Number of WL Additions")
	graph export "ln`x'_NonHCV_ES.eps", as(eps) replace	
	
}

******************************


************************************************************************************************************
** Now WL removal analyses *********************************************************************************
************************************************************************************************************

use "$workdir\outputdata\estimation sample.dta", clear

//drop if wl_org=="KI"
drop if !inrange(can_rem_year,2005,2019)
drop if missing(can_listing_opo_id)
drop if wl_org=="KI" & can_dgn_hep==1 // drops any KI candidates that were marked as HCV-related diagnosis in data prep file
replace can_dgn_hep = 1 if wl_org=="KI"
drop if missing(can_dgn_hep) // this drops LI candidates with diagnosis codes that are neither classified as hcv or non-hcv

gen daystotx = rec_tx_dt - can_listing_dt
sum daystotx if wl_org=="LI" & can_rem_year<2013, detail
local medLIdtt = r(p50)
sum daystotx if wl_org=="KI" & can_rem_year<2013, detail
local medKIdtt = r(p50)

gen txfasterthanmedian = 0 
replace txfasterthanmedian = 1 if daystotx<=`medLIdtt' & wl_org=="LI"
replace txfasterthanmedian = 1 if daystotx<=`medKIdtt' & wl_org=="KI"


collapse (sum) toosickdied improved ddtx ldtx tx (mean) daystotx txfasterthanmedian, by(can_dgn_hep wl_org can_listing_opo_id can_rem_year)


fillin can_dgn_hep wl_org can_listing_opo_id can_rem_year

drop if _fillin==1 & wl_org=="KI" & can_dgn_hep==0 // no need to have HCV/non-HCV for KI 

* drop OPOs that never have LI activity
bysort can_listing_opo_id wl_org: egen avgfillin = mean(_fillin)
drop if avgfillin==1 & wl_org=="LI" // this drops 5 OPOs: 416, 422, 427, 460, 471

foreach x in toosickdied improved ddtx ldtx tx {
	replace `x' = 0 if missing(`x')
	gen ln`x' = ln(1+`x')
}

gen lndaystotx = ln(1+daystotx)

encode wl_org, gen(wl_org1)

** making it so that KI is retained as control group when specifying HCV vs. Non-HCV groups in regressions
gen can_dgn_nothep = 0 if wl_org=="LI" & can_dgn_hep==1
replace can_dgn_nothep = 1 if wl_org=="KI"
replace can_dgn_nothep = 1 if can_dgn_hep==0 & wl_org=="LI"

gen year = can_rem_year

gen daa = 0
replace daa = 1 if year>=2014 & wl_org=="LI"

sort can_listing_opo_id can_rem_year wl_org can_dgn_hep

foreach x in 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 {
gen treated`x' = 0
replace treated`x' = 1 if wl_org=="LI" & year==`x'
} 

gen zero = 0

gen wl_year = can_rem_year
merge m:1 can_listing_opo_id wl_year wl_org using "$workdir/outputdata/WL composition by OPO and year.dta" // this brings in size and composition variables
drop if _merge!=3

foreach x in toosickdied improved tx ddtx ldtx {
	gen `x'_pct = `x'/numonwlhcv if can_dgn_hep==1 
	replace `x'_pct = `x'/numonwlnonhcv if can_dgn_hep==0 
	replace `x'_pct = `x'/numonwl if wl_org=="KI"
}	


*initialize outreg file
global outregfilename wlremovals_dsayear_KI_DiD
reg tx year
outreg2 using $outregfilename.xls, excel stats(coef se) keep(year) dec(4) pdec(4) replace


***************************************
*** Table 2, column 2 *****************
*** and Appendix Figure 3a and 3b *****
***************************************


foreach x in tx {

	** HCV
	reghdfe `x'_pct daa if can_dgn_hep==1, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa) dec(4) pdec(4) append

	sum `x'_pct if wl_org=="LI" & e(sample)
	sum `x'_pct if wl_org=="LI" & e(sample) & inrange(year,2005,2013)

	reghdfe `x'_pct treated2005 treated2006 treated2007 treated2008 treated2009 treated2010 treated2011 zero treated2013 ///
	treated2014 treated2015 treated2016 treated2017 treated2018 treated2019 if can_dgn_hep==1, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append
	est sto `x'_hcv

	** Non-HCV
	reghdfe `x'_pct daa if can_dgn_nothep==1, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa) dec(4) pdec(4) append

	sum `x'_pct if wl_org=="LI" & e(sample)
	sum `x'_pct if wl_org=="LI" & e(sample) & inrange(year,2005,2013)

	reghdfe `x'_pct treated2005 treated2006 treated2007 treated2008 treated2009 treated2010 treated2011 zero treated2013 ///
	treated2014 treated2015 treated2016 treated2017 treated2018 treated2019 if can_dgn_nothep==1, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append
	est sto `x'_nonhcv

}
	
	coefplot tx_hcv, omitted keep(treated2005 treated2006 treated2007 treated2008 treated2009 treated2010 treated2011 zero treated2013 treated2014 treated2015 treated2016 treated2017 treated2018 treated2019) ///
	coeflabel(treated2005 = "2005" treated2006 = "2006" treated2007 = "2007" treated2008 = "2008" treated2009 = "2009" treated2010 = "2010" treated2011 = "2011" zero = "2012" treated2013 = "2013" ///
	treated2014 = "2014" treated2015 = "2015" treated2016 = "2016" treated2017 = "2017" treated2018 = "2018" treated2019 = "2019") ///
	vertical yline(0) xline(9.5) title("Effect of DAAs on HCV Transplant Rate") xtitle(Year) ytitle("Transplant Rate")
	graph export "tx_HCV_ES_pct.eps", as(eps) replace
	
	coefplot tx_nonhcv, omitted keep(treated2005 treated2006 treated2007 treated2008 treated2009 treated2010 treated2011 zero treated2013 treated2014 treated2015 treated2016 treated2017 treated2018 treated2019) ///
	coeflabel(treated2005 = "2005" treated2006 = "2006" treated2007 = "2007" treated2008 = "2008" treated2009 = "2009" treated2010 = "2010" treated2011 = "2011" zero = "2012" treated2013 = "2013" ///
	treated2014 = "2014" treated2015 = "2015" treated2016 = "2016" treated2017 = "2017" treated2018 = "2018" treated2019 = "2019") ///
	vertical yline(0) xline(9.5) title("Effect of DAAs on Non-HCV Transplant Rate") xtitle(Year) ytitle("Transplant Rate")
	graph export "tx_NonHCV_ES_pct.eps", as(eps) replace	
	
******************************

	
*********************************
*** Table 2, column 1 ***********
*** and Figure 5a and 5c ********
*********************************

foreach x in tx {

	** HCV
	reghdfe ln`x' daa if can_dgn_hep==1, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa) dec(4) pdec(4) append

	sum `x' if wl_org=="LI" & e(sample)
	sum `x' if wl_org=="LI" & e(sample) & inrange(year,2005,2013)

	reghdfe ln`x' treated2005 treated2006 treated2007 treated2008 treated2009 treated2010 treated2011 zero treated2013 ///
	treated2014 treated2015 treated2016 treated2017 treated2018 treated2019 if can_dgn_hep==1, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append
	est sto `x'_hcv

	** Non-HCV
	reghdfe ln`x' daa if can_dgn_nothep==1, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa) dec(4) pdec(4) append

	sum `x' if wl_org=="LI" & e(sample)
	sum `x' if wl_org=="LI" & e(sample) & inrange(year,2005,2013)

	reghdfe ln`x' treated2005 treated2006 treated2007 treated2008 treated2009 treated2010 treated2011 zero treated2013 ///
	treated2014 treated2015 treated2016 treated2017 treated2018 treated2019 if can_dgn_nothep==1, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append
	est sto `x'_nonhcv

}
	
	coefplot tx_hcv, omitted keep(treated2005 treated2006 treated2007 treated2008 treated2009 treated2010 treated2011 zero treated2013 treated2014 treated2015 treated2016 treated2017 treated2018 treated2019) ///
	coeflabel(treated2005 = "2005" treated2006 = "2006" treated2007 = "2007" treated2008 = "2008" treated2009 = "2009" treated2010 = "2010" treated2011 = "2011" zero = "2012" treated2013 = "2013" ///
	treated2014 = "2014" treated2015 = "2015" treated2016 = "2016" treated2017 = "2017" treated2018 = "2018" treated2019 = "2019") ///
	vertical yline(0) xline(9.5) title("Effect of DAAs on Log Transplants to HCV Registrants") xtitle(Year) ytitle("Log Number of Transplants")
	graph export "tx_HCV_ES_log.eps", as(eps) replace
	
	coefplot tx_nonhcv, omitted keep(treated2005 treated2006 treated2007 treated2008 treated2009 treated2010 treated2011 zero treated2013 treated2014 treated2015 treated2016 treated2017 treated2018 treated2019) ///
	coeflabel(treated2005 = "2005" treated2006 = "2006" treated2007 = "2007" treated2008 = "2008" treated2009 = "2009" treated2010 = "2010" treated2011 = "2011" zero = "2012" treated2013 = "2013" ///
	treated2014 = "2014" treated2015 = "2015" treated2016 = "2016" treated2017 = "2017" treated2018 = "2018" treated2019 = "2019") ///
	vertical yline(0) xline(9.5) title("Effect of DAAs on Log Transplants to Non-HCV Registrants") xtitle(Year) ytitle("Log Number of Transplants")
	graph export "tx_NonHCV_ES_log.eps", as(eps) replace	
	
*************************

	
**********************************
***** Appendix Table 5 ***********
**********************************

*initialize outreg file
global outregfilename timetooutcome_dsayear_KI_DiD
reg tx year
outreg2 using $outregfilename.xls, excel stats(coef se) keep(year) dec(4) pdec(4) replace


foreach x in daystotx {

	** HCV
	reghdfe ln`x' daa if can_dgn_hep==1, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa) dec(4) pdec(4) append

	sum `x' if wl_org=="LI" & e(sample)
	sum `x' if wl_org=="LI" & e(sample) & inrange(year,2005,2013)


	** Non-HCV
	reghdfe ln`x' daa if can_dgn_nothep==1, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa) dec(4) pdec(4) append

	sum `x' if wl_org=="LI" & e(sample)
	sum `x' if wl_org=="LI" & e(sample) & inrange(year,2005,2013)

}

foreach x in txfasterthanmedian {

	** HCV
	reghdfe `x' daa if can_dgn_hep==1, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa) dec(4) pdec(4) append

	sum `x' if wl_org=="LI" & e(sample)
	sum `x' if wl_org=="LI" & e(sample) & inrange(year,2005,2013)

	
	** Non-HCV
	reghdfe `x' daa if can_dgn_nothep==1, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa) dec(4) pdec(4) append

	sum `x' if wl_org=="LI" & e(sample)
	sum `x' if wl_org=="LI" & e(sample) & inrange(year,2005,2013)

}

*************************

	

*******************************************************************
** Now Non-HCV Transplants Triple-Diff

use "$workdir\outputdata\estimation sample.dta", clear

//drop if wl_org=="KI"
drop if !inrange(can_rem_year,2005,2019)
drop if missing(can_listing_opo_id)

gen rec_hcv_li = 0 if rec_hcv_stat_li=="N"
replace rec_hcv_li = 1 if rec_hcv_stat_li=="P"
replace rec_hcv_li = . if !inrange(can_rem_year,2005,2013)
bysort can_listing_opo_id: egen hcvposrate = mean(rec_hcv_li)

drop if can_dgn_hep==1 // focusing only on non-HCV folks
drop if missing(can_dgn_hep) & wl_org=="LI" // this drops LI candidates with diagnosis codes that are neither classified as hcv or non-hcv

collapse (sum) toosickdied improved ddtx ldtx tx (firstnm) hcvposrate, by(wl_org can_listing_opo_id can_rem_year)

fillin wl_org can_listing_opo_id can_rem_year

* drop OPOs that never have LI activity
bysort can_listing_opo_id wl_org: egen avgfillin = mean(_fillin)
drop if avgfillin==1 & wl_org=="LI" // this drops 5 OPOs: 416, 422, 427, 460, 471

encode wl_org, gen(wl_org1)

foreach x in toosickdied improved ddtx ldtx tx {
	replace `x' = 0 if missing(`x')
	gen ln`x' = ln(1+`x')
}

gen year = can_rem_year

gen daa = 0
replace daa = 1 if year>=2014 & wl_org=="LI"

sort can_listing_opo_id can_rem_year wl_org

foreach x in 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 {
gen treated`x' = 0
replace treated`x' = 1 if wl_org=="LI" & year==`x'
} 

gen zero = 0

gen daahcvpos = hcvposrate*daa

sum hcvposrate, detail
local medposrate = r(p50)

gen hcvposrate_abovemed = 0
replace hcvposrate_abovemed = 1 if hcvposrate>=`medposrate'

gen daahcvposrate_abovemed = daa*hcvposrate_abovemed


******************************************
**** Table 3, Panel B ********************
******************************************

*initialize outreg file
global outregfilename wlremovals_dsayear_KI_DiD_nonhcv_triplediff
reg tx year
outreg2 using $outregfilename.xls, excel stats(coef se) keep(year) dec(4) pdec(4) replace


foreach x in tx {

	reghdfe ln`x' daa daahcvpos, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa daahcvpos) dec(4) pdec(4) append
		
	sum `x' if wl_org=="LI" & e(sample)
	sum `x' if wl_org=="LI" & e(sample) & inrange(year,2005,2013)

	reghdfe ln`x' daa if hcvposrate>=`medposrate' & !missing(hcvposrate), absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa) dec(4) pdec(4) append
		
	sum `x' if wl_org=="LI" & e(sample)
	sum `x' if wl_org=="LI" & e(sample) & inrange(year,2005,2013)

	reghdfe ln`x' daa if hcvposrate<`medposrate' & !missing(hcvposrate), absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa) dec(4) pdec(4) append
		
	sum `x' if wl_org=="LI" & e(sample)
	sum `x' if wl_org=="LI" & e(sample) & inrange(year,2005,2013)
	
}

*************************************	
	
*******************************************************************
** Now Non-HCV WL Adds Triple-Diff

use "$workdir\outputdata\estimation sample.dta", clear

//drop if wl_org=="KI"
drop if !inrange(can_listing_year,2005,2019)
drop if missing(can_listing_opo_id)

gen rec_hcv_li = 0 if rec_hcv_stat_li=="N"
replace rec_hcv_li = 1 if rec_hcv_stat_li=="P"
replace rec_hcv_li = . if !inrange(can_rem_year,2005,2013)
bysort can_listing_opo_id: egen hcvposrate = mean(rec_hcv_li)

drop if can_dgn_hep==1 // focusing only on non-HCV folks
drop if missing(can_dgn_hep) & wl_org=="LI" // this drops LI candidates with diagnosis codes that are neither classified as hcv or non-hcv

gen wladd = 1 if !missing(wl_org)

collapse (sum) wladd (firstnm) hcvposrate, by(wl_org can_listing_opo_id can_listing_year)

fillin wl_org can_listing_opo_id can_listing_year

* drop OPOs that never have LI activity
bysort can_listing_opo_id wl_org: egen avgfillin = mean(_fillin)
drop if avgfillin==1 & wl_org=="LI" // this drops 5 OPOs: 416, 422, 427, 460, 471


foreach x in wladd {
	replace `x' = 0 if missing(`x')
	gen ln`x' = ln(1+`x')
}


encode wl_org, gen(wl_org1)

gen year = can_listing_year

gen daa = 0
replace daa = 1 if year>=2014 & wl_org=="LI"

sort can_listing_opo_id can_listing_year wl_org

foreach x in 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 {
gen treated`x' = 0
replace treated`x' = 1 if wl_org=="LI" & year==`x'
} 

gen zero = 0

gen daahcvpos = hcvposrate*daa

sum hcvposrate, detail
local medposrate = r(p50)

gen hcvposrate_abovemed = 0
replace hcvposrate_abovemed = 1 if hcvposrate>=`medposrate'

gen daahcvposrate_abovemed = daa*hcvposrate_abovemed


******************************************
**** Table 3, Panel A ********************
******************************************

*initialize outreg file
global outregfilename wladds_dsayear_KI_DiD_nonhcv_triplediff
reg wladd year
outreg2 using $outregfilename.xls, excel stats(coef se) keep(year) dec(4) pdec(4) replace


foreach x in wladd {

	reghdfe ln`x' daa daahcvpos, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa daahcvpos) dec(4) pdec(4) append
		
	sum `x' if wl_org=="LI" & e(sample)
	sum `x' if wl_org=="LI" & e(sample) & inrange(year,2005,2013)

	reghdfe ln`x' daa if hcvposrate>=`medposrate' & !missing(hcvposrate), absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa) dec(4) pdec(4) append
		
	sum `x' if wl_org=="LI" & e(sample)
	sum `x' if wl_org=="LI" & e(sample) & inrange(year,2005,2013)

	reghdfe ln`x' daa if hcvposrate<`medposrate' & !missing(hcvposrate), absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa) dec(4) pdec(4) append
		
	sum `x' if wl_org=="LI" & e(sample)
	sum `x' if wl_org=="LI" & e(sample) & inrange(year,2005,2013)
	
}

******************************************


*************************************************************************
** Now donor disposition analyses

use "$workdir\outputdata\donor disposition sample.dta", clear


foreach x in all all_hcv {
	gen ln`x' = ln(1+`x')
}

gen all_hcv_rate = all_hcv/all

foreach x in poorqual {
    gen `x'rate = `x'/all
	gen ln`x' = ln(1+`x')
    gen `x'hcvrate = `x'_hcv/all_hcv
}

gen don_org = "LI" if organagg==5
replace don_org = "KI" if organagg==2

gen daa = 0
replace daa = 1 if year>=2014 & don_org=="LI"

foreach x in 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 {
gen treated`x' = 0
replace treated`x' = 1 if don_org=="LI" & year==`x'
} 

gen zero = 0

set scheme s1mono
set graphics on // set on to view graphics
graph set window fontface "Times New Roman" // Looks good in Journals
graph set eps fontface Times // Looks Good in Journals



******************************************
*** Appendix Table 7 *********************
******************************************

*initialize outreg file
global outregfilename disposition_dsayear_KI_DiD
reg all year
outreg2 using $outregfilename.xls, excel stats(coef se) keep(year) dec(4) pdec(4) replace


foreach x in poorqual {

	reghdfe ln`x' daa , absorb(don_opo_ctr_id#organagg year) cluster(don_opo_ctr_id#organagg)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa) dec(4) pdec(4) append
	
	sum `x' if don_org=="LI" & e(sample)
	sum `x' if don_org=="LI" & e(sample) & inrange(year,2005,2013)
	
	
	reghdfe `x'rate daa , absorb(don_opo_ctr_id#organagg year) cluster(don_opo_ctr_id#organagg)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa) dec(4) pdec(4) append
	
	sum `x'rate if don_org=="LI" & e(sample)
	sum `x'rate if don_org=="LI" & e(sample) & inrange(year,2005,2013)
	
	
	reghdfe `x'hcvrate daa , absorb(don_opo_ctr_id#organagg year) cluster(don_opo_ctr_id#organagg)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(daa) dec(4) pdec(4) append
	
	sum `x'hcvrate if don_org=="LI" & e(sample)
	sum `x'hcvrate if don_org=="LI" & e(sample) & inrange(year,2005,2013)
	
}

************************************

cap log close

